/* 
 * @author 朱家进
 * @version 1.4
 * 2007-01-11
 */
package com.zjht.gmcc.bean.group;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;



public class GroupCheckFile {
	
	private Statement stmt;
    private ResultSet rs;
    private String msg = "";
        
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public GroupCheckFile (){
//		this.msg = "";
//		
//		Connection conn = myJDBC..getConnection();
//		 try {
//			stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			setMsg("数据库连接错误，请联系管理员");
//		}
		
	}	

	public List check(String path,String filename){
		List lists = new ArrayList();
		File file = new File(path+filename);
		try {
			//FileInputStream instream = new FileInputStream(file);
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = "";
            String bossFilename = "";
			String bossFilesize = "";
            
			while((line=reader.readLine())!=null){
				GroupCheckFileText text = new GroupCheckFileText();
				bossFilename = line.split(",")[0];
				bossFilesize = line.split(",")[1];
				text.setFilename(bossFilename);
				text.setFilesize(bossFilesize);
		        lists.add(text); 		
			}
			reader.close();			
		} catch (FileNotFoundException e) {
          setMsg("对帐文件不存在"); 			
		} catch (IOException e) {

			//setMsg("IO出错");	
		}
		return lists;
	}

	//�ȴ��ı��ļ��ж�ȡ��¼����ÿһ�����ݿ��¼��Ƚ�
	public List getCMResults(String sql,String path,String name){
		List ltext = new ArrayList();
		List entitys = new ArrayList();
		List finalLists = new ArrayList();
		String flag = "";
		String filename = "";
		String filesize = "";
		String lfilename = "";
		String lfilesize = "";
		
		ltext = check(path, name);
		try {			
			rs = stmt.executeQuery(sql);
			while(rs.next()){				
				filename = rs.getString("filename");
				filesize = rs.getString("bytes");
				GroupCheckFileEntity entity = new GroupCheckFileEntity();
				entity.setFilename(filename);
				entity.setBytes(filesize);
				entitys.add(entity);
			}

		} catch (SQLException e) {
			//setMsg("SQL出错");
		}
		
		for(int j = 0;j<ltext.size();j++){
			flag = "0";
			lfilename = ((GroupCheckFileText)ltext.get(j)).getFilename();
			lfilesize = ((GroupCheckFileText)ltext.get(j)).getFilesize();
			
			for(int i = 0;i < entitys.size();i++ ){
				filename = ((GroupCheckFileEntity)entitys.get(i)).getFilename();
				filesize = ((GroupCheckFileEntity)entitys.get(i)).getBytes();				
				if(lfilename.equals(filename) && lfilesize.equals(filesize)){
				    flag = "1";	
				}			
			}
			
			GroupCheckFileView bean = new GroupCheckFileView();
			if(flag.equals("1")){
				bean.setFilename(lfilename);
				bean.setBytes(lfilesize);
				bean.setTfilename(lfilename);
				bean.setTfilesize(lfilesize);
				bean.setFlag(flag);														
			}else{
				bean.setFilename("");
				bean.setBytes("");
				bean.setTfilename(lfilename);
				bean.setTfilesize(lfilesize);
				bean.setFlag(flag);					
			}						
			finalLists.add(bean);					
		}

		return finalLists;
	}
}
